package com.cetc.sdp.kmga.cs.schema.v2;

import com.alibaba.fastjson.JSONArray;
import com.cetc.sdp.kmga.cs.common.TableMapping;
import com.cetc.sdp.kmga.cs.schema.SchemaDesc;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author： DengQiang
 * @date: 2018/2/7 15:14
 */
@Deprecated
public class IndexSchemaDesc<T> implements SchemaDesc {

    private List<TableMapping.Column>  columns;

    private int fieldCount;

    /**
     * 字段索引
     */
    public T logFieldIndex;

    public IndexSchemaDesc(TableMapping tm, Class<T> tClass) {
        columns = new ArrayList<>();
        int size = tm.getColumnMap().size();
        for (int i = 0; i < size; i++) {
            columns.add(null);
        }
        Map<String, TableMapping.Column> columnMap = new HashMap<>(tm.getColumnMap());
        try {
            T instance = tClass.newInstance();
            this.logFieldIndex = instance;
            //使用反射进行初始化
            Field[] fields = tClass.getFields();
            //在HBase表中，新增的字段，原日志中没有
            //新增字段的索引为-1
            Map<String, Field> extendFields = new HashMap<>(32);

            for (Field field : fields) {
                TableMapping.Column column = columnMap.remove(field.getName());
                if (column != null) {
                    if (column.getMappingIndex() > -1) {
                        field.setInt(instance, column.getMappingIndex());
                        fieldCount++;
                        columns.set(column.getMappingIndex(), column);
                    } else {
                        extendFields.put(field.getName(), field);
                    }
                }
            }
            //为新增字段设置一个索引
            for (Map.Entry<String, Field> entry : extendFields.entrySet()) {
                entry.getValue().setInt(instance, fieldCount);
                columns.set(fieldCount++, tm.getColumn(entry.getKey()));
            }
            //处理没有在LogFieldIndex的字段
            for (TableMapping.Column column : columnMap.values()){
                if (column.getMappingIndex() > 0 ) {
                    columns.set(fieldCount++, column);
                }
            }
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }

    public List<String> buildLog(JSONArray array) {
        List<String> res  = new ArrayList<>(fieldCount);
        for (int i = 0; i < array.size(); i++) {
            res.add(i, array.getString(i));
        }
        for (int i = array.size(); i < fieldCount; i++) {
            res.add(i, "");
        }
        return res;
    }

    public int getFieldCount() {
        return fieldCount;
    }

    @Override
    public List<TableMapping.Column> getColumns() {
        return columns;
    }

    @Override
    public int getColumnIdx(String columnName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public TableMapping.Column getColumnByIndex(int columnIdx) {
        if (columnIdx < 0 || columnIdx > columns.size()) {
            return null;
        } else {
            return columns.get(columnIdx);
        }
    }
}
